<script setup>
import axios from 'axios'
import { ref, onMounted } from 'vue'
import { addDeptApi, queryById, queryAllApi, updateById, delById } from '../../api/dept'
import { ElMessage } from 'element-plus'

const dialogFormVisible = ref(false)
const tableData = ref([])
const titleName = ref('')
const search = async () => {
  const res = await queryAllApi()
  tableData.value = res.data
}
onMounted(() => {
  search()
})


const formData = ref({ name: '' })
const deptFormRef = ref(null)
const add = () => {
  dialogFormVisible.value = true
  titleName.value = '新增数据'
  formData.value = { name: '' }
}
const formRules = ref({
  name: [
    { required: true, message: '请输入部门名称', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ]
})
const resetForm = () => {
  deptFormRef.value.resetFields()
}
const save = async () => {
  await deptFormRef.value.validate(async valid => {
    if (!valid) return
    let result = null
    if (formData.value.id) {
      result = await updateById(formData.value)
    } else {
      result = await addDeptApi(formData.value)
    }
    if (result.code) {
      ElMessage.success('操作成功')
      dialogFormVisible.value = false
      resetForm()
      search()
    } else {
      ElMessage.error(result.msg)
    }
  })
}
const query = async (row) => {
  titleName.value = '修改数据'
  dialogFormVisible.value = true
  formData.value = { name: '' }
  const res = await queryById(row.id)
  formData.value = res.data
}

const del = async (row) => {
  const res = await delById(row.id)
  if (res.code) {
    ElMessage.success('操作成功')
    search()
  } else {
    ElMessage.error('操作失败')
  }
}
</script>

<template>
  <div>
    <el-dialog v-model="dialogFormVisible" :title=titleName width="500">
      <el-form :model="formData" :rules="formRules" ref="deptFormRef">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">关闭</el-button>
          <el-button type="primary" @click="save()">
            提交
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>

  <el-form-item>
    <el-button type="primary" @click="add">新增</el-button>
  </el-form-item>
  <br />
  <br />
  <br />


  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="id" label="id" width="180" />
    <el-table-column prop="name" label="姓名" width="180" />
    <el-table-column prop="createTime" label="创建时间" width="180" />
    <el-table-column prop="updateTime" label="更新时间" width="180" />
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button link type="primary" size="small" @click="query(row)">编辑</el-button>
        <el-button link type="primary" size="small" @click="del(row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<style scoped></style>
